﻿@model IEnumerable<PurchaseOrderInstallment>
@{
    var purchaseOrder = (PurchaseOrder) ViewBag.PurchaseOrder;
    var serviceDN = new KPIS.GERP.WEB.GIMS.DeliveryNoteService.DeliveryNoteServiceClient();
     
} 

@if(purchaseOrder == null)
{
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    <div class="alert alert-block alert-error">ไม่มีข้อมูลใบสั่งซื้อนี้ (#@ViewBag.HeaderPoId)</div>
} else {
    
    
    var items = new KPIS.GERP.WEB.GIMS.PurchaseOrderItemService.PurchaseOrderItemServiceClient().
        Select(new PurchaseOrderItem { PoId = purchaseOrder.PoId }, new string[1] { "BidderQuotation" });

    var poSumtotal = items.Sum(o => o.BidderQuotation != null ? o.BidderQuotation.NetAmount : 0);
    
    
    ViewBag.Title = "การแบ่งงวด";
    ViewBag.Descr = "เพื่อทำการแบ่งงวดของใบสั่ง<span class='text-error'>\"" + purchaseOrder.Name + "\"</span>";
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";
    using (Html.BeginForm("DeleteAll", "Installment", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        @Html.Hidden("HeaderPoId", purchaseOrder.PoId)
        <div class="row-fluid">
            @if (ViewBag.ErrorMessage != null)
            {
            }
            <div class="block-button-menu-above">
                @*@if (ViewBag.bEditable)
                {
                    
                }*@
                @Html.ActionLink("เพิ่ม", "Create", new {HeaderPoId = purchaseOrder.PoId}, new {@class = "btn btn-small btn-success button-standard-width", role = "button"})
                @Html.ButtonDeleteAll()
            </div>
             
            <div class="alert-error">@Html.ValidationSummary()</div>
            <div class="row-fluid">
                <div class="table-header span12">
                    <div class="span10">@ViewBag.Title</div>
                    @Html.Partial("_ExportButton")
                </div>
            </div>
            <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info" width="100%">
                <thead>
                    <tr>
                        <th class="center" style="width: 50px;"><label><input type="checkbox" /><span class="lbl"></span></label></th>
                        <th class="center" style="width: 100px;">งวดที่</th>
                        <th class="center">เลขการตรวจรับ</th>
                        <th class="center">ส่งมอบภายใน</th>
                        <th class="center">@Html.DisplayNameFor(model => model.FirstOrDefault().DeliveryDate)</th>
                        <th class="center">วงเงินสุทธิ (บาท)</th>                    
                        <th class="center">@Html.DisplayNameFor(model => model.FirstOrDefault().Remarks)</th>
                        <th class="center" style="width: 80px;">&nbsp;</th>
                    </tr>
                </thead>
                <tbody>                
                    @{
        
            int countPoInstallDeliveried = 0;
            int countTotalPoInstallDeliveried = 0;
            decimal sumPoInstallDeliveried = 0;
            decimal sumTotalPoInstallDeliveried = 0;  
                        foreach (var m in Model ?? new List<PurchaseOrderInstallment>())
                        {
                            var dnOfPoInstall = serviceDN.SelectDeliveryNote(new DeliveryNote() { PoInstallId = m.PoInstallId, RecordStatus = "!" + RecordStatus.DeletedStatus }, null).FirstOrDefault();
                            
                            <tr>
                                <td class="center">
                                    <!-- Check box all for delete multiple records -->
                                    @*<label><input type="checkbox" name="deleteIds" value="@(m.PoInstallId)" /><span class="lbl"></span></label>*@

                                    @if (dnOfPoInstall != null)
                                    {
                                        <label>
                                            <input type="checkbox" name="deleteIds" value="@(m.PoInstallId)" disabled />
                                            <span class="lbl"></span>
                                        </label>
                                    }else{
                                        <label>
                                            <input type="checkbox" name="deleteIds" value="@(m.PoInstallId)" />
                                            <span class="lbl"></span>
                                        </label>
                                    }

                                </td>
                                <td style="font-weight: bold; text-align: center">
                                    @Html.ActionLink("งวดที่ " + m.InstallNumber, "Details", new {HeaderPoId = purchaseOrder.PoId, Id = m.PoInstallId})
                                </td>
                                <td>
                                    @{
                                        sumTotalPoInstallDeliveried += m.TotalAmount;
                                        countTotalPoInstallDeliveried++;
                                    }
                                    @if (dnOfPoInstall != null)
                                    {
                                        countPoInstallDeliveried++;
                                        sumPoInstallDeliveried += m.TotalAmount;
                                        <a href="@Url.Action("Details", "DeliveryNote", new {dnOfPoInstall.DnId, PoId=dnOfPoInstall.PoId, PoInstallId=dnOfPoInstall.PoInstallId, ViewBag.NoLayout, ViewBag.ViewOnly})" target="_blank">
                                            <u>
                                                @dnOfPoInstall.Code
                                                (@KPIS.GERP.Models.SYS.RecordStatus.RecordStatusName(dnOfPoInstall.RecordStatus))
                                                <i class="icon-external-link"></i>
                                            </u>
                                        </a>
                                    }
                                    else
                                    {
                                        @:ยังไม่ตรวจรับ
                                    }
                                </td>
                                <td>
                                    <a href="@Url.Action("Details", new {HeaderPoId = purchaseOrder.PoId, Id = m.PoInstallId})">
                                        @if (m.DurTypeId == 8)
                                        {
                                            @:@Html.Raw(m.RequireDuration.HasValue ? Html.DisplayFor(model => m.RequireDuration).ToString() : "0") วัน นับจากวันที่ทำใบสั่ง
                                        }
                                        else
                                        {
                                            @:@(m.RequireDate.HasValue ? m.RequireDate.Value.ToString("d MMM yyyy") : "")
                                        }
                                    </a>
                                </td>
                                <td>
                                    @if (m.DeliveryDate != null)
                                    {
                                        @m.DeliveryDate.Value.ToString("d MMM yyyy")
                                    }
                                    else if (dnOfPoInstall != null && dnOfPoInstall.DeliveryDate != null)
                                    {
                                        @dnOfPoInstall.DeliveryDate.Value.ToString("d MMM yyyy")
                                    }
                                    else
                                    {
                                        @:-
                                    }
                                </td>
                                <td>@((m.TotalAmount).ToString("#,##0.00")) </td>						
                                <td>@Html.DisplayFor(model => m.Remarks)</td>
                                <td class="td-actions">
                                    
                                    @if (dnOfPoInstall == null)
                                    {
                                        @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                                        {
                                            Id = m.PoInstallId,
                                            DetailsUrl = Url.Action("Details", new { HeaderPoId = purchaseOrder.PoId, Id = m.PoInstallId }),
                                            EditUrl = Url.Action("Edit", new { HeaderPoId = purchaseOrder.PoId, Id = m.PoInstallId }),
                                            DeleteUrl = Url.Action("Delete", new { HeaderPoId = purchaseOrder.PoId, Id = m.PoInstallId })
                                        })
                                    }
                                    
                                </td>
                            </tr>
                        }
                    }
                </tbody>
            </table>
        </div>
        <div style="font-size:large;color:blue;text-align:right;padding-right:7em;">

            ยอดรวมในรายการตรวจรับ = @sumTotalPoInstallDeliveried.ToString("#,##0.00") บาท<br />

            ยอดรวมในใบสั่งซื้อ/จ้าง = @((poSumtotal??0).ToString("#,##0.00")) บาท<br />           
            <br />  
            โดยมีการตรวจรับไปแล้ว = @countPoInstallDeliveried ครั้ง @sumPoInstallDeliveried.ToString("#,##0.00") บาท  
            จากทั้งหมด @countTotalPoInstallDeliveried งวด
        </div>
    }
}

@section scripts {
    
    <script type="text/javascript">
        $(function() {
            var oTable1 = $('#table').dataTable({
                "aoColumns": [
                    { "bSortable": false }, null, null, null, null, null, null, { "bSortable": false }
                ],
                "iDisplayLength": -1
            });

            $('table th input:checkbox').on('click', function() {
                var that = this;
                $(this).closest('table').find('tr > td:first-child input:checkbox')
                    .each(function() {
                        this.checked = that.checked;
                        $(this).closest('tr').toggleClass('selected');
                    });
            });

            $('[data-rel=tooltip]').tooltip();

            $("form").submit(function() {
                var currentForm = this;
                bootbox.confirm(function(result) {
                    var _result = result;
                    if (result) {
                        $.ajax({
                            url: "@Url.Action("DeleteAll")",
                            type: "post",
                            dataType: "json",
                            data: $(currentForm).serialize(),
                            success: function(result2) {
                                if (result2.success) {
                                    location.reload();
                                    $("#message").html(result2.message);
                                } else {
                                    $("#message").html(result2.message);
                                }
                            }
                        });
                    }
                });
                return false;
            });
        })
    </script>
}